﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using DBAccess;
using SSPharma.Common;
using SSPharma.View;
using SSPharma.ViewModel;

namespace SSPharma
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            Application.Current.ShutdownMode = System.Windows.ShutdownMode.OnExplicitShutdown;

            if (InitializeDB() == ConnectionState.Open)
            {

                frmDangNhap wDangNhap = new frmDangNhap();

                // Create the ViewModel to which 
                // the main window binds.
                var loginViewModel = new DangNhapViewModel();

                // Allow all controls in the window to 
                // bind to the ViewModel by setting the 
                // DataContext, which propagates down 
                // the element tree.
                wDangNhap.DataContext = loginViewModel;


                if (wDangNhap.ShowDialog() == true)
                {
                    
                    frmMainWindow window = new frmMainWindow();
                                        
                    // Create the ViewModel to which 
                    // the main window binds.
                    var viewModel = new MainWindowViewModel();

                    // Allow all controls in the window to 
                    // bind to the ViewModel by setting the 
                    // DataContext, which propagates down 
                    // the element tree.
                    window.DataContext = viewModel;
                    
                    window.Show();
                }
                else
                {
                    Application.Current.Shutdown();
                }

            }
            else
            {
                MessageBox.Show("Cannot connect to DB!");
                Application.Current.Shutdown();
            }
        }

        #region Private Methods

        private ConnectionState InitializeDB()
        {
            XMLConfig Config = XMLConfig.Load();
            Global.DonVi = Config.Header.DonVi;
            Global.ORG = Config.Header.ORG;
            Global.DataFactory = new SQLServer();
            Global.DataFactory.Login_User = MD5Crypt.Decrypt(Config.Database.UID,"SSPharmaglomed",true);
            Global.DataFactory.Login_Pass = MD5Crypt.Decrypt(Config.Database.PWD, "SSPharmaglomed", true);
            Global.DataFactory.Login_Host = Config.Database.SERVER;
            Global.DataFactory.Login_DBName = MD5Crypt.Decrypt(Config.Database.DATABASE,"SSPharmaglomed", true);
            Global.DataFactory.Login_mode = SQLServer.mode.Server;

            return Global.DataFactory.Connect();

        }

        #endregion
    }
}
